package com.jxx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jxx.mapper.AddressMapper;
import com.jxx.mapper.ItemMapper;
import com.jxx.mapper.OrderMapper;
import com.jxx.pojo.Address;
import com.jxx.pojo.Item;
import com.jxx.pojo.Order;
import com.jxx.pojo.PageObject;
import com.jxx.service.OrderService;
import com.jxx.service.UserService;
import com.jxx.vo.ItemDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private AddressMapper addressMapper;

    @Override
    /**
     * @Description: 订单分页查询
     * @Param: [userId, orderStatus, page, rows]
     * @return: com.jxx.pojo.PageObject<com.jxx.pojo.Order>
     * @Author: laixinghua
     * @Date: 2020/12/8 0008 下午 2:31
     */
    public PageObject<Order> findAllOrderByUserIdAndOrderStatus(Integer userId, Integer orderStatus,Integer page,Integer rows   )
    {
        IPage<Order> iPage = new Page<>(page,rows);
        QueryWrapper<Order> queryWrapper=new QueryWrapper();

        if(orderStatus==0)
        {
            queryWrapper.eq("user_id", userId).orderByDesc("update_time");//状态为o查询所有订单
        }else {
            queryWrapper.eq("user_id", userId).eq("order_status", orderStatus).orderByDesc("update_time");
        }

        iPage = orderMapper.selectPage(iPage,queryWrapper);
        List<Order> orders = iPage.getRecords();
        List<Order> orderList=new ArrayList<>();
        //新建一个数组用来接遍历后的item，不能用原数组接，因为原数组在遍历时不允许进行增删操作，会报ConcurrentModificationException异常
        for (Order order:orders){
            Item item = itemMapper.selectById(order.getItemId());
            order.setItem(item);
            orderList.add(order);
        }

        return new PageObject<Order>(iPage.getTotal(),orderList );
    }

    @Override
    /**
     * @Description: 订单删除
     * @Param: [orderId]
     * @return: int
     * @Author: laixinghua
     * @Date: 2020/12/8 0008 下午 2:31
     */
    public int deleteOrderByOrderId(Integer orderId) {
        int i = orderMapper.deleteById(orderId);
        return i;
    }

    @Override
    /**
     * @Description: 更改订单状态
     * @Param: [order]
     * @return: int
     * @Author: laixinghua
     * @Date: 2020/12/8 0008 下午 2:34
     */
    public int updateOrderStatus(Order order) {
        int i = orderMapper.updateById(order);
        return i;
    }

    @Override
    /**
    * @Description: 根据订单ID找订单详情
    * @Param: [orderId]
    * @return: com.jxx.pojo.Order
    * @Author: laixinghua
    * @Date: 2020/12/8 0008 下午 8:37
    */
    public Order findorderDetailByorderId(Integer orderId) {
        Order order = orderMapper.selectById(orderId);
        Item item = itemMapper.selectById(order.getItemId());
        order.setItem(item);
        Address address = addressMapper.selectById(order.getAddressId());
        order.setAddress(address);
        return order;
    }
}
